/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import entity.MaterialFacade;
import entity.MaterialFacadeLocal;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author User
 */
public class Material extends HttpServlet {
    
    @EJB
    private MaterialFacadeLocal materialFacade = new MaterialFacade();

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Material</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Material at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String idText = request.getParameter("id");
        int id = Integer.parseInt(idText);
        entity.Material Material = materialFacade.find(id);
       
        XStream xstream = new XStream(new DomDriver());
        xstream.alias("Material", entity.Material.class);
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root>";        
        
        xml += xstream.toXML(Material); 
        xml += "</root>";                
                       
        response.setContentType("text/xml"); 
        PrintWriter out = response.getWriter();
        out.println(xml);
        
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String id = request.getParameter("id");
        String nombre = request.getParameter("name-producto");
        String descripcion = request.getParameter("descripcion");        
        String costo = request.getParameter("costo-producto");
        String cantidad = request.getParameter("cantidad-producto");
        String accion = request.getParameter("accion");        
        entity.Material material;                
        if(id.isEmpty() || nombre.isEmpty() || descripcion.isEmpty() || costo.isEmpty() || cantidad.isEmpty()){                        
            //request.getRequestDispatcher("faces/admin/index.xhtml#inventario-4").forward(request, response);
            //response.sendRedirect("faces/admin/index.xhtml#inventario-4");
        }else if(accion.equals("Crear")){
            material = new entity.Material();            
            material.setIdmaterial(Integer.parseInt(id));
            material.setNombre(nombre);
            material.setDescripcion(descripcion);
            material.setCantidad(Integer.parseInt(cantidad));
            material.setCosto(Double.parseDouble(costo));                                        
            materialFacade.create(material);
        }else if(accion.equals("Modificar")){            
            material = materialFacade.find(Integer.parseInt(id));
            material.setNombre(nombre);
            material.setDescripcion(descripcion);
            material.setCantidad(Integer.parseInt(cantidad));
            material.setCosto(Double.parseDouble(costo));            
            materialFacade.edit(material);                      
        }else if(accion.equals("Borrar")){                                      
            material = materialFacade.find(Integer.parseInt(id));
            materialFacade.remove(material);
        }            
        HttpSession session = request.getSession();
        //session.setAttribute("materiales", materialFacade.findAll());
        response.sendRedirect("faces/admin/index.xhtml#inventario-4");
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
